package com.travel.haoma.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ImageView;

/**
 * Created by 磊 on 2015/9/4.
 */
public class EditImageView extends ImageView {
    public static final int MODE_FILTER = 1;
    public static final int MODE_EDIT = 2;
    public static final int STATE_MOVE = 1;
    public static final int STATE_SCALE = 2;
    public static final int STATE_NONE = 0;
    private int mode = MODE_FILTER;
    private Rect rect ;
    private Paint paint = new Paint();
    private int state = STATE_NONE;
    public void setMode(int mode){
        this.mode = mode;
        invalidate();
    }
    public EditImageView(Context context) {
        super(context);
    }
    public EditImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    private float lastX;
    private float lastY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                    if(mode == MODE_EDIT){
                        lastX = event.getX();
                        lastY = event.getY();
                        if(Math.abs(lastX-rect.right)<=moveSlop&&Math.abs(lastY-rect.bottom)<=moveSlop){
                            state = STATE_SCALE;
                        }else if(rect.contains((int)lastX,(int)lastY)){
                            state = STATE_MOVE;
                        }else state = STATE_NONE;

                    }
                break;
            case MotionEvent.ACTION_MOVE:
                if(state == STATE_SCALE){
                    if(event.getX()-rect.left<30|event.getY()-rect.top<30|event.getX()>=getWidth()|event.getY()>=getHeight()){
                        return true;
                    }
                    rect.right = (int) event.getX();
                    rect.bottom = (int) event.getY();
                    invalidate();
                }else if(state == STATE_MOVE){

                    int delX = (int) (event.getX()-lastX);
                    int delY = (int) (event.getY() - lastY);
                    if(rect.left+delX<=0|rect.top+delY<=0|rect.right+delX>=getWidth()|rect.bottom+delY>=getHeight()){
                        lastX = event.getX();
                        lastY = event.getY();
                        return false;
                    }
                    rect = new Rect(rect.left+delX,rect.top+delY,rect.right+delX,rect.bottom+delY);
                    lastX = event.getX();
                    lastY = event.getY();
                    invalidate();
                }
                break;
        }
        return true;
    }
    private int moveSlop;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mode == MODE_EDIT){
            if(rect==null) {
                rect = new Rect();
                rect.set(getWidth()/4,getMeasuredHeight()/4, (int) (getWidth()*3f/4), (int) (getMeasuredHeight() * 3f / 4));
                moveSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
            }
            paint = new Paint();
            paint.setColor(Color.BLUE);
            paint.setStrokeWidth(2);
            canvas.drawLine(rect.left, rect.top, rect.right, rect.top, paint);
            canvas.drawLine(rect.left, rect.top, rect.left, rect.bottom, paint);
            canvas.drawLine(rect.left, rect.bottom, rect.right, rect.bottom, paint);
            canvas.drawLine(rect.right, rect.top, rect.right, rect.bottom, paint);

            paint.setColor(Color.parseColor("#66000000"));
            canvas.drawRect(new Rect(0, 0, getWidth(), rect.top), paint);
            canvas.drawRect(new Rect(0,rect.bottom,getWidth(),getHeight()) ,paint);
            canvas.drawRect(new Rect(0,rect.top,rect.left,rect.bottom) ,paint);
            canvas.drawRect(new Rect(rect.right,rect.top,getWidth(),rect.bottom) ,paint);
        }
    }
}
